package com.kaki;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;

/**
 * @author Kaki Nakajima
 * @Date 2019-08-26
 * @desc 执行类
 */
public class XlSScanner implements ActionListener {

    private static String selectedPath = "";
    private static JTextField scanPathfield = null;
    private static JTextField keybroudfield = null;

    public static void main(String[] args) throws MalformedURLException {

        //标题
        JLabel scanPathfield_label = new JLabel("扫描路径（xls所在文件夹）");
        JLabel keybroudfield_label = new JLabel("关键字 （请以“,”分隔）");

        //输入框
        scanPathfield = new JTextField();
        keybroudfield = new JTextField();

        //按钮组
        JButton open = new JButton("选择文件夹");
        final JButton scan = new JButton("开始扫描");
        scan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("==========>开始扫描（参天大树）<==========");
                System.out.println("==========>关键字："+keybroudfield.getText());

                try{
                    String[] arr = keybroudfield.getText().split(",");
                    StringBuilder sb = new StringBuilder();
                    HashSet<String> set = new HashSet<String>();
                    selectedPath = scanPathfield.getText();
                    XLSRearder.getFileNames(selectedPath,set);
                    for (String fileName : set) {
                        XLSRearder.readXls(fileName, sb,selectedPath+"\\"+fileName,arr);
                    }

                    //扫描成功后提示
                    AlertFrame.alert(sb.toString());
                    System.out.println("========>扫描结束（参天大树）<======="+sb.toString());

                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });


        //盒子模型
        Box userInputBox = Box.createVerticalBox();
        Box buttonBox = Box.createHorizontalBox();
        Box scanPathBox = Box.createHorizontalBox();
        Box keybroudBox = Box.createHorizontalBox();

        //搜索路径盒子
        scanPathBox.add(scanPathfield_label);
        scanPathBox.add(Box.createHorizontalStrut(10));
        scanPathBox.add(scanPathfield);

        //搜索关键字盒子
        keybroudBox.add(keybroudfield_label);
        keybroudBox.add(Box.createHorizontalStrut(21));
        keybroudBox.add(keybroudfield);

        //总输入框盒子
        userInputBox.add(scanPathBox);
        userInputBox.add(keybroudBox);

        //总按钮盒子
        buttonBox.add(Box.createHorizontalStrut(108));
        buttonBox.add(open);
        scan.setBackground(Color.magenta);
        open.setBackground(Color.cyan);
        buttonBox.add(Box.createHorizontalStrut(111));
        buttonBox.add(scan);

        //打开文件选择器
        open.addActionListener(new XlSScanner());

        userInputBox.setBounds(20,80,600,60);
        buttonBox.setBounds(80,200,400,100);

        //总弹框
        JFrame window1 = new JFrame("参天大树xls扫描工具(第四代)");
        Container container = window1.getContentPane();
        container.setBackground(new Color(204,238,200));
        window1.setBounds(60,100,700,400);
        window1.setVisible(true);
        window1.setLayout(null);

        //替换标志
        URL url = new URL("https://portrait.gitee.com/uploads/avatars/user/1720/5160819_kakiNakajima_1578981461.png!avatar60");
        ImageIcon icon = new ImageIcon(url);
        window1.setIconImage(icon.getImage());

        //总JFrame 与 文本域合并
        window1.add(userInputBox);
        window1.add(buttonBox);
        window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        window1.validate();

    }

    //打开文件系统
    public void actionPerformed(ActionEvent e) {

        JFileChooser jFileChooser = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jFileChooser.showDialog(new JLabel(), "选择");
        File file = jFileChooser.getSelectedFile();
        selectedPath = file.getAbsolutePath();
        scanPathfield.setText(file.getAbsolutePath());
        System.out.println("文件："+file.getAbsolutePath());

    }
}
